package com.yange.accountbook.popup

import android.content.Context
import android.text.InputType
import android.text.TextUtils
import android.widget.Button
import android.widget.EditText
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.lxj.xpopup.core.BottomPopupView
import com.yange.accountbook.R
import com.yange.accountbook.entity.IconEntity
import com.yange.accountbook.net.Api
import com.yange.accountbook.net.BaseResponse
import com.yange.accountbook.net.api.RetrofitClient
import com.yange.accountbook.net.observable.RecObserver
import com.yange.accountbook.ui.adapter.IconAdapter
import com.yange.accountbook.utils.showToast
import java.util.stream.Collectors

class IconPopup(context: Context) : BottomPopupView(context) {


    var addListener: AddListener? = null
    lateinit var icon: IconEntity
    lateinit var icons: MutableList<IconEntity>
    lateinit var iconAdapter: IconAdapter
    lateinit var listIcon: RecyclerView
    lateinit var editName: EditText
    lateinit var btnCommit: Button

    interface AddListener {
        fun success(icon: IconEntity)
    }

    constructor(context: Context, addListener: AddListener, icon: IconEntity) : this(context) {
        this.addListener = addListener
        this.icon = icon
    }

    override fun getImplLayoutId() = R.layout.pop_add_icon_layout


    override fun onCreate() {
        super.onCreate()
        popupInfo.autoOpenSoftInput = false
        listIcon = findViewById(R.id.list_icon)
        editName = findViewById(R.id.edit_book_name)
        btnCommit = findViewById(R.id.btn_commit)

        editName.setText(icon.name)
        editName.clearFocus()
        editName.setInputType(InputType.TYPE_NULL)

        listIcon.layoutManager = GridLayoutManager(context, 5)
        iconAdapter = IconAdapter()
        listIcon.adapter = iconAdapter

        getIconList()

        initListener()
    }

    fun initListener() {
        iconAdapter.setOnItemClickListener { adapter1, view, position ->
            iconAdapter.setChecked(position)
        }
        btnCommit.setOnClickListener {
            if (iconAdapter.checkedPosition == -1) {
                showToast("请选择图标")
            } else if (TextUtils.isEmpty(editName.text)) {
                showToast("请输入名称")
            } else {
                var i = icons[iconAdapter.checkedPosition]
                i.name = editName.text.toString().trim()
                i.bizId = icon.bizId
                addListener?.success(i)
                dismiss()
            }
        }
        editName.setOnTouchListener { _, event ->
            editName.setInputType(InputType.TYPE_CLASS_TEXT)
            editName.onTouchEvent(event)
            true
        }
    }

    fun getIconList() {
        RetrofitClient.getInstance().invokeGet(context, Api.GET_ICON)
            .subscribe(object :
                RecObserver<BaseResponse<List<String>>>(context, true, false) {
                override fun onSuccess(t: BaseResponse<List<String>>?) {
                    icons = t!!.data.stream().map {
                        val entity = IconEntity()
                        entity.icon = it
                        return@map entity
                    }.collect(Collectors.toList())
                    iconAdapter.setNewInstance(icons)
                    if (icon.icon.isNotBlank()) {
                        var position = t!!.data.getItemPositionByName(icon.icon);
                        iconAdapter.setChecked(position)
                        listIcon.layoutManager?.scrollToPosition(position)
                    }
                }

                override fun onFail(msg: String, code: Int) {

                }
            })
    }

    fun List<String>.getItemPositionByName(item: String): Int {
        this.forEachIndexed { index, it ->
            if (it == item)
                return index
        }
        return 0
    }
}